<template>
  <el-dialog
    :before-close="handleClose"
    :close-on-click-modal="false"
    title="创建新规则"
    :visible.sync="dialogVisible"
    width="70%"
  >
    <el-form
      ref="yaraForm"
      v-loading="loading"
      label-width="100px"
      :model="yaraForm"
      :rules="rules"
      size="mini"
    >
      <el-form-item label="规则名" prop="name">
        <el-input
          v-model="yaraForm.name"
          placeholder="请输入规则名"
          spellcheck="false"
        />
      </el-form-item>

      <div>
        <el-form-item
          v-permission="['super', 'org']"
          label="创建者"
          prop="user_id"
        >
          <el-select v-model="yaraForm.user_id" filterable>
            <el-option
              v-for="(item, index) in userList"
              :key="index"
              :label="`${item.last_name}${item.first_name} (${item.email})`"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
      </div>

      <el-form-item label="初始状态" prop="disabled">
        <el-switch v-model="yaraForm.disabled" />
      </el-form-item>

      <div>
        <el-form-item v-permission="['super']" label="公共" prop="public">
          <el-switch v-model="yaraForm.public" />
        </el-form-item>
      </div>

      <el-form-item label="规则描述" prop="abstract">
        <el-input
          v-model="yaraForm.abstract"
          :autosize="{ minRows: 3, maxRows: 20 }"
          spellcheck="false"
          type="textarea"
        />
      </el-form-item>
      <el-form-item label="yara" prop="yara">
        <el-input :autosize="{ minRows: 10, maxRows: 20 }" type="textarea" />
        <code-mirror v-model="yaraForm.yara" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submit('yaraForm')">保存</el-button>
        <el-button @click="dialogVisible = false">返回</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import clearWatch from "@/utils/mixin/clearWatch";
import user from "@/api/user";
import antivirus from "@/api/antivirus";
import CodeMirror from "@/components/module/codeMirror.vue";
import { mapGetters } from "vuex";
export default {
  name: "ToastNewYara",
  components: { CodeMirror },
  mixins: [clearWatch],
  data() {
    return {
      dialogVisible: false,
      loading: false,
      org_id: "",
      userList: [],
      yaraFormOption: {
        yara: "",
        user_id: "",
        name: "",
        abstract: "",
        public: true,
        disabled: true
      },
      yaraForm: {
        yara: "",
        user_id: "",
        name: "",
        abstract: "",
        public: true,
        disabled: true
      },
      rules: {
        name: [{ required: true, message: "请输入规则名", trigger: "blur" }],
        yara: [{ required: true, message: "请输入yara", trigger: "blur" }],
        abstract: [
          { required: true, message: "请输入规则描述", trigger: "blur" }
        ],
        user_id: [
          { required: true, message: "请选择创建者", trigger: "change" }
        ],
        disabled: [
          { required: true, message: "请选择初始状态", trigger: "change" }
        ],
        public: [
          { required: true, message: "请选择公共状态", trigger: "change" }
        ]
      }
    };
  },
  computed: mapGetters(["UserInfo"]),
  methods: {
    handleClose(done) {
      done();
    },
    show(org_id) {
      this.dialogVisible = !this.dialogVisible;

      this.org_id = org_id;
      this.loadingData();
    },

    async loadingData() {
      if (this.UserInfo.role_en === "user") {
        this.yaraForm.user_id = this.UserInfo.id;
      } else {
        await this.loadingUser();
      }
    },

    loadingUser() {
      return user.users({ org_id: this.org_id }).then(res => {
        this.userList = res.data.users;
      });
    },

    submit(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          let data = Object.assign({}, this.yaraForm);
          data.disabled = !data.disabled;
          antivirus.create(data).then(res => {
            this.$message.success(res.data.message);
            this.dialogVisible = false;
            this.yaraForm = Object.assign({}, this.yaraFormOption);
            this.$emit("change");
          });
        } else {
          return false;
        }
      });
    }
  }
};
</script>
